#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "diord.h"
#include "main_win.h"
#include "digital.h"

#define MAXNUM 10
#define MESG1 "请输入1至10的整数！"
#define MESG2 "请输入正确的序列！"
#define MESG3 "此序列是最大序列，不存在下一个序列！"

static GtkWidget *window, *num_entry, *val_entry, *res_label1, *res_label2;

static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data){
	return FALSE;
}

static void destroy(GtkWidget *widget, gpointer data){
	gtk_widget_show (m_window);
	gtk_widget_hide (window);
}

static void clicked(GtkWidget *widget, GtkEntry *entry){
	int i, n = 0, num, ret, *order;
	char *num_text, *val_text, *start;
	GtkWidget *mesg;

	/*取要排序的数的数目*/
	num_text = (char *)gtk_entry_get_text ((GtkEntry *)num_entry);
	num = digital (num_text, &n);

	if(num <= 0 || num > MAXNUM){
		mesg = gtk_message_dialog_new ((GtkWindow *)window, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_NONE, MESG1);
		gtk_widget_show (mesg);
		return;
	}

	/*取输入的排序*/
	start = val_text = (char *)gtk_entry_get_text ((GtkEntry *)val_entry);
	order = malloc (sizeof(int) * num);
	for(i = 0, n = 0; i < num; i++){
		ret = digital (start, &n);

		int j = 0;
		while(order[j] != ret && j < i)
			j++;

		if(j != i || ret <= 0 || ret > num){
			mesg = gtk_message_dialog_new ((GtkWindow *)window, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_NONE, MESG2);
			gtk_widget_show (mesg);
			free(order);
			return;
		}

		order[i] = ret;
		start = start + n;
		n = 0;
	}

	/*终于可以正式处理了！！调用diord()*/
	if(NOTFOUND == diord (num, order)){
		mesg = gtk_message_dialog_new ((GtkWindow *)window, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_NONE, MESG3);
		gtk_widget_show (mesg);
		free(order);
		return;
	}
	free(order);
}

/*界面。简单的界面也要这么多代码，哎……*/
int diord_win(void){
	GtkWidget *num_label, *val_label, *tips_label1, *tips_label2, *button, *hbox1, *hbox2, *hbox3, *hbox4, *vbox;

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW(window), DIORD);
	gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
	gtk_window_set_icon_from_file (GTK_WINDOW(window), LOGO, NULL);
	gtk_window_set_default_size (GTK_WINDOW(window), 1, 1);
	gtk_window_set_resizable (GTK_WINDOW(window), FALSE);

	g_signal_connect (window, "delete-event", G_CALLBACK (delete_event), NULL);
	g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);

	num_label = gtk_label_new ("请输入数目：");
	num_entry = gtk_entry_new();

	val_label = gtk_label_new ("请输入一个排列：");
	val_entry = gtk_entry_new();

	res_label1 = gtk_label_new (NULL);
	res_label2 = gtk_label_new (NULL);
	tips_label1 = gtk_label_new ("字典序值为：");
	tips_label2 = gtk_label_new ("下一个排列：");

	hbox1 = gtk_hbox_new (FALSE, 0);
	hbox2 = gtk_hbox_new (FALSE, 0);
	hbox3 = gtk_hbox_new (FALSE, 0);
	hbox4 = gtk_hbox_new (FALSE, 0);
	vbox = gtk_vbox_new (FALSE, 0);

	button = gtk_button_new_with_label ("确定");
	g_signal_connect (button, "clicked", G_CALLBACK (clicked), NULL);

	gtk_box_pack_start (GTK_BOX (hbox1), num_label, FALSE, FALSE, 0);
	gtk_widget_show (num_label);

	gtk_box_pack_start (GTK_BOX (hbox1), num_entry, FALSE, FALSE, 0);
	gtk_widget_show (num_entry);

	gtk_box_pack_start (GTK_BOX (hbox2), val_label, FALSE, FALSE, 0);
	gtk_widget_show (val_label);

	gtk_box_pack_start (GTK_BOX (hbox2), val_entry, FALSE, FALSE, 0);
	gtk_widget_show (val_entry);

	gtk_box_pack_start (GTK_BOX (hbox3), tips_label1, FALSE, FALSE, 0);
	gtk_widget_show (tips_label1);

	gtk_box_pack_start (GTK_BOX (hbox3), res_label1, FALSE, FALSE, 0);
	gtk_widget_show (res_label1);

	gtk_box_pack_start (GTK_BOX (hbox4), tips_label2, FALSE, FALSE, 0);
	gtk_widget_show (tips_label2);

	gtk_box_pack_start (GTK_BOX (hbox4), res_label2, FALSE, FALSE, 0);
	gtk_widget_show (res_label2);

	gtk_box_pack_start (GTK_BOX (hbox2), button, FALSE, FALSE, 0);
	gtk_widget_show (button);
	
	gtk_box_pack_start (GTK_BOX (vbox), hbox1, FALSE, FALSE, 0);
	gtk_widget_show (hbox1);

	gtk_box_pack_start (GTK_BOX (vbox), hbox2, FALSE, FALSE, 0);
	gtk_widget_show (hbox2);

	gtk_box_pack_start (GTK_BOX (vbox), hbox3, FALSE, FALSE, 0);
	gtk_widget_show (hbox3);

	gtk_box_pack_start (GTK_BOX (vbox), hbox4, FALSE, FALSE, 0);
	gtk_widget_show (hbox4);

	gtk_container_add (GTK_CONTAINER (window), vbox);
	gtk_widget_show (vbox);

	gtk_widget_show (window);

	return 0;
}

/*输出结果，diord()中调用*/
int diord_prin(int val, int *order, int n){
	int i, count = 0;
	char *val_str, *order_str;

	val_str = (char *)malloc (sizeof(char) * n);
	order_str = (char *)malloc (sizeof(char) * n * 6);

	snprintf (val_str, sizeof(char) * n, "%d", val);
	for(i = 0; i < n; i++)
		count += snprintf(&(order_str[count]), 6, "%d ", order[i]);

	gtk_label_set_text ((GtkLabel *)res_label1, val_str);
	gtk_label_set_text ((GtkLabel *)res_label2, order_str);

	free (val_str);
	free (order_str);

	return 0;
}
